package com.bindapp;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.bindapp.activity.LoginActivity;
import com.bindapp.databinding.ActivityMainBinding;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.CALL_PHONE

    };//Manifest.permission.REQUEST_INSTALL_PACKAGES

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        validePermissions();
    }

    private void turnToLogin(){
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void validePermissions(){
        if(AndPermission.hasPermissions(MainActivity.this,PERMISSIONS_STORAGE)){
            Log.e("Mytext","hasPermissions");
            //openapk();
            turnToLogin();

        }else{
            AndPermission.with(MainActivity.this)
                    .runtime()
                    .permission(PERMISSIONS_STORAGE)
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            Log.e("Mytext","onGranted");
                            turnToLogin();
                        }
                    })
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            Log.e("Mytext","onDenied");
                        }
                    })
                    .start();
        }
        //1.udp网络请求做了并发请求封装。2.登陆页面做完 接口已接通。3注册页面做完接口还木有，4首页第一个tab页面已经好了。需要细调,还木有接口,5.第二个tab已经画好
    }

    private void openapk(){
        File apkfile = new File(Environment.getExternalStorageDirectory().getPath()+ File.separator+"mg.apk");
        if(apkfile.exists()){
            AndPermission.with(MainActivity.this)
                    .install()
                    .file(apkfile)
                    .rationale(new Rationale<File>() {
                        @Override
                        public void showRationale(Context c, File f, RequestExecutor e) {
                            e.execute();
                            // 取消启动：e.cancel();
                        }
                    })
                    .onGranted(new Action<File>() {
                        @Override
                        public void onAction(File data) {
                        }
                    })
                    .onDenied(new Action<File>() {
                        @Override
                        public void onAction(File data) {
                        }
                    })
                    .start();
        }
    }
}
